package com.mjk.common.biz;

import java.util.List;
import java.util.Map;
import java.lang.reflect.ParameterizedType;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mjk.common.util.EntityUtils;
import com.mjk.common.util.Query;
import com.mjk.common.vo.TableResponse;
import com.mjk.model.system.BaseGroup;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

public abstract class BaseBiz<M extends Mapper<T>, T>{
	
	@Autowired
	protected M mapper;
	
	public M getMapper() {
		return mapper;
	}

	public void setMapper(M mapper) {
		this.mapper = mapper;
	}


	public int insertSelective(T arg0){
		EntityUtils.setCreatAndUpdatInfo(arg0);
		return mapper.insertSelective(arg0);
	}

	
	public int deleteByExample(Object arg0){
		return mapper.deleteByExample(arg0);
	}

	
	public int updateByPrimaryKey(T arg0){
		EntityUtils.setUpdatedInfo(arg0);
		return mapper.updateByPrimaryKey(arg0);
	}

	
	public int selectCount(T arg0){
		return mapper.selectCount(arg0);
	}

	
	public int deleteByPrimaryKey(Object arg0){
		return mapper.deleteByPrimaryKey(arg0);
	}

	
	public int insert(T arg0){
		EntityUtils.setCreatAndUpdatInfo(arg0);
		return mapper.insert(arg0);
	}

	
	public int updateByExampleSelective(T arg0, Object arg1){
		EntityUtils.setUpdatedInfo(arg0);
		return mapper.updateByExampleSelective(arg0, arg1);
	}

	
	public int updateByPrimaryKeySelective(T arg0){
		EntityUtils.setUpdatedInfo(arg0);
		return mapper.updateByPrimaryKeySelective(arg0);
	}

	
	public List<T> selectByExampleAndRowBounds(Object arg0, RowBounds arg1){
		return mapper.selectByExampleAndRowBounds(arg0, arg1);
	}

	
	public T selectOne(T arg0){
		return mapper.selectOne(arg0);
	}

	
	public List<T> selectAll(){
		return mapper.selectAll();
	}

	
	public int selectCountByExample(Object arg0){
		return mapper.selectCountByExample(arg0);
	}

	
	public int updateByExample(T arg0, Object arg1){
		EntityUtils.setUpdatedInfo(arg0);
		return mapper.updateByExample(arg0, arg1);
	}

	
	public T selectByPrimaryKey(Object arg0){
		return mapper.selectByPrimaryKey(arg0);
	}

	
	public int delete(T arg0){
		return mapper.delete(arg0);
	}

	
	public List<T> select(T arg0){
		return mapper.select(arg0);
	}

	
	public List<T> selectByExample(Object arg0){
		return mapper.selectByExample(arg0);
	}

	
	public List<T> selectByRowBounds(T arg0, RowBounds arg1){
		return mapper.selectByRowBounds(arg0, arg1);
	}
	
	public TableResponse<T> selectByQuery(Query query){ 
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
	    Example example = new Example(clazz);
	    Example.Criteria criteria = example.createCriteria();
	    for (Map.Entry<String, Object> entry : query.entrySet()) {
	        criteria.andLike(entry.getKey(), "%" + entry.getValue().toString() + "%");
	    }
	    Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
	    List<T> list = mapper.selectByExample(example);
	    return new TableResponse<T>(result.getTotal(), list);
    }
}
